<?php

abstract class InheritableActiveRecord extends CActiveRecord{
	
	public function afterConstruct()
	{
		if($this->getScenario()=='insert'){
			$relations = $this->relations();
			$this->super = new $relations['super'][1];
		}
		parent::afterConstruct();
	}
	
	/**
	 * PHP getter magic method.
	 * This method is overridden so that AR attributes can be accessed like properties.
	 * @param string $name property name
	 * @return mixed property value
	 * @see getAttribute
	 */
	public function __get($name)
	{
		try{
			return parent::__get($name);
		}catch(CException $e){
			return $this->super->__get($name);
		}
	}
	
	public function __set($name, $value)
	{
		try{
			return parent::__set($name, $value);
		}catch(CException $e){
			return $this->super->__set($name, $value);
		}
	}
	
	public function save($runValidation=true,$attributes=null)
	{
		$this->super->save($runValidation, $attributes);
		if($this->getScenario()=='insert'){
			$key = $this->primaryKey();
			$this->$key = $this->super->id;
		}
		parent::save($runValidation, $attributes);
	}
	
	public function primaryKey()
	{
		$relations = $this->relations();
		return $relations['super'][2];
	}
}
